# -*- coding: utf-8 -*-
import scrapy
from scrapy_redis.spiders import RedisSpider
from fenbushi_lianjia.items import FenbushiLianjiaItem


class LianjiaSpider(RedisSpider):
    name = 'lianjia'
    allowed_domains = ['fang.lianjia.com']
    # start_urls = ['https://bj.fang.lianjia.com/loupan/pg1/']
    redis_key = "lianjia:fangzi"

    def parse(self, response):
        li_list = response.xpath("//div[@class='resblock-list-wrapper']/li")
        for li in li_list:
            item = FenbushiLianjiaItem()
            title = li.xpath(".//div[@class='resblock-name']/a/@title").extract_first()
            type_ = li.xpath(".//div[@class='resblock-name']/span[1]/text()").extract_first()
            info_url = "https://bj.fang.lianjia.com" + li.xpath("//div[@class='resblock-name']/a/@href").extract_first()
            item["title"] = title
            item["type_"] = type_
            yield scrapy.Request(info_url, callback=self.info_parse, meta={"item": item})

    def info_parse(self, response):
        item = response.meta["item"]
        unit_price = response.xpath("/html/body/div[2]/div[3]/div[2]/div/div[2]/div[1]/span[2]/text()").extract_first()
        price = response.xpath('/html/body/div[2]/div[3]/div[2]/div/div[2]/div[1]/span[4]/text()').extract_first()
        house_type = response.xpath(
            "/html/body/div[2]/div[3]/div[2]/div/div[3]/ul/li[3]/span[2]/span[1]/text()").extract_first()
        site = response.xpath("/html/body/div[2]/div[3]/div[2]/div/div[3]/ul/li[1]/span[2]/text()").extract_first()
        item["unit_price"] = unit_price
        item["price"] = price
        item["house_type"] = house_type
        item["site"] = site
        yield item
